use std::thread;
fn main() {
    let xx = |x| x;
    let result = xx(5);
    println!("The result is {}", result);

    // 尝试调用一个被推断为两个不同类型的闭包
    // let result = xx("Hello, world!");
    // println!("The result is {}", result);

    // 捕获引用或者移动所有权
    let mut list = vec![1, 2, 3];
    println!("Before defining closure: {list:?}");

    let only_borrows = || println!("From closure: {list:?}");

    println!("Before calling closure: {list:?}");
    only_borrows();
    println!("After calling closure: {list:?}");

    let mut reflist = || list.push(4);
    // println!("Before calling closure: {list:?}");
    reflist();
    println!("After calling closure: {list:?}");

    let list2 = list.iter().map(|x| x * 2).collect::<Vec<i32>>();
    println!("After collecting: {list2:?}");

    thread::spawn(move || println!("From thread: {list:?}"))
        .join()
        .unwrap();

    // print!("After thread: {list:?}"); // error: value borrowed here after move

}
